package com.xjscrm.console.param;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjscrm.common.constant.CommonConstant;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * 查询参数
 */
@ApiModel("分页参数")
public class QueryParam implements Serializable {
	private static final long serialVersionUID = 7305222607977143006L;
	@ApiModelProperty("当前页")
	private Integer current = CommonConstant.DEFAULT_PAGE_INDEX;
    @ApiModelProperty("页大小")
	private Integer size = CommonConstant.DEFAULT_PAGE_SIZE;
    @ApiModelProperty("排序字段")
	private String orderByField;
    @ApiModelProperty("是否升序")
	private boolean isAsc;
    @ApiModelProperty("关键字搜索")
    private String keyword;

	public QueryParam() {

    }

    public Integer getCurrent() {
        return current;
    }

    public void setCurrent(Integer current) {
	    if (current == null || current == 0){
	        this.current = CommonConstant.DEFAULT_PAGE_INDEX;
        }else{
            this.current = current;
        }
    }

    public Integer getSize() {
        return size;
    }

    public void setSize(Integer size) {
	    if (size == null || size == 0){
	        this.size = CommonConstant.DEFAULT_PAGE_SIZE;
        }else{
            this.size = size;
        }
    }

    public String getOrderByField() {
        return orderByField;
    }

    public void setOrderByField(String orderByField) {
        this.orderByField = orderByField;
    }

    public boolean isAsc() {
        return isAsc;
    }

    public void setAsc(boolean asc) {
        isAsc = asc;
    }

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    public void convert(Page page){
        page.setCurrent(this.getCurrent());
        page.setSize(this.getSize());
        OrderItem orderItem = null;
        if (orderByField!=null){
            if (this.isAsc()){
                page.addOrder(OrderItem.asc(orderByField));
            }else{
                page.addOrder(OrderItem.desc(orderByField));
            }
        }
    }

    @Override
    public String toString() {
        return "QueryParam{" +
                "current=" + current +
                ", size=" + size +
                ", orderByField='" + orderByField + '\'' +
                ", isAsc=" + isAsc +
                ", keyword='" + keyword + '\'' +
                '}';
    }
}
